import 'package:flutter/material.dart';

import '../../resources/resources.dart';
import '../my_image.dart';
import '../my_svg.dart';
import '../my_text.dart';

class MyIconTextButton extends StatelessWidget {
  final VoidCallback? onPressed;
  final Widget icon;
  final String text;
  final Color? color;
  final Color? textColor;
  final Color? backgroundColor;
  final bool isEnable;
  final double size;
  //间隔
  final double gap;
  final double textSize;
  final double? iconSize;
  final double? radius;
  final double elevation;

  const MyIconTextButton({
    Key? key,
    required this.icon,
    required this.text,
    required this.onPressed,
    this.color,
    this.textColor,
    this.backgroundColor,
    this.isEnable = true,
    this.size = 60,
    this.gap = 4,
    this.textSize = 12,
    this.radius,
    this.elevation = 0,
    this.iconSize,
  }) : super(key: key);

  factory MyIconTextButton.img(
    String path, {
    required String text,
    double? size,
    double? gap,
    double? textSize,
    double iconSize = 24,
    double? radius,
    double elevation = 0,
    Color? color,
    Color? textColor,
    Color? backgroundColor,
    bool isEnable = true,
    required VoidCallback? onPressed,
  }) {
    return MyIconTextButton(
      icon: MyImage(
        path,
        width: iconSize,
        height: iconSize,
        color: color,
      ),
      text: text,
      onPressed: onPressed,
      color: color,
      textColor: textColor,
      backgroundColor: backgroundColor,
      isEnable: isEnable,
      size: size ?? 60,
      gap: gap ?? 4,
      textSize: textSize ?? 12,
      radius: radius,
      elevation: elevation,
      iconSize: iconSize,
    );
  }

  factory MyIconTextButton.icon(
    IconData iconData, {
    required String text,
    double? size,
    double? gap,
    double? textSize,
    double? iconSize,
    double? radius,
    double elevation = 0,
    Color? color,
    Color? textColor,
    Color? backgroundColor,
    bool isEnable = true,
    required VoidCallback onPressed,
  }) {
    return MyIconTextButton(
      icon: Icon(
        iconData,
        size: iconSize,
        color: color,
      ),
      text: text,
      onPressed: onPressed,
      color: color,
      textColor: textColor,
      backgroundColor: backgroundColor,
      isEnable: isEnable,
      size: size ?? 60,
      gap: gap ?? 4,
      textSize: textSize ?? 12,
      radius: radius,
      elevation: elevation,
      iconSize: iconSize,
    );
  }

  factory MyIconTextButton.svg(
    String path, {
    required String text,
    double? size,
    double? gap,
    double? textSize,
    double? iconSize,
    double? radius,
    double elevation = 0,
    Color? color,
    Color? textColor,
    Color? backgroundColor,
    bool isEnable = true,
    required VoidCallback? onPressed,
  }) {
    return MyIconTextButton(
      icon: MySvg(
        path,
        width: iconSize,
        height: iconSize,
        color: color,
      ),
      text: text,
      onPressed: onPressed,
      color: color,
      textColor: textColor,
      backgroundColor: backgroundColor,
      isEnable: isEnable,
      size: size ?? 60,
      gap: gap ?? 4,
      textSize: textSize ?? 12,
      radius: radius,
      elevation: elevation,
      iconSize: iconSize,
    );
  }

  Widget _widget() {
    Widget iconWidget = icon;
    if (icon is Icon) {
      final icon2 = icon as Icon;
      iconWidget = Icon(
        icon2.icon,
        size: icon2.size,
        color: icon2.color ?? color,
      );
    }

    return SizedBox(
      width: size,
      height: size,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          iconSize != null
              ? Container(
                  width: iconSize,
                  height: iconSize,
                  alignment: Alignment.center,
                  child: iconWidget,
                )
              : iconWidget,
          gapv(gap),
          MyText(
            text,
            size: textSize,
            color: textColor ?? color,
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: backgroundColor ?? Colors.transparent,
      borderRadius: BorderRadius.circular(radius ?? size / 2),
      elevation: elevation,
      child: InkWell(
        borderRadius: BorderRadius.circular(radius ?? size / 2),
        onTap: isEnable ? onPressed : null,
        child: isEnable
            ? _widget()
            : AnimatedOpacity(
                opacity: 0.5,
                duration: const Duration(milliseconds: 300),
                child: _widget()),
      ),
    );
  }
}
